﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ConcurrantLoadingLib;

namespace ConcurrantLoadingConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            var stopwatch = new Stopwatch();
            stopwatch.Start();
            Console.WriteLine("LOAD START");
            
            var trades = new Trades();

            var tasks = new List<Task>();
            tasks.Add(Task.Factory.StartNew(() =>
                                                {
                                                    foreach (var trade in new TradeLoader().GetTrades(@"..\..\TradeData\TradesOne.csv"))
                                                        trades.Add(trade);
                                                }));

            tasks.Add(Task.Factory.StartNew(() =>
                                                {
                                                    foreach (var trade in new TradeLoader().GetTrades(@"..\..\TradeData\TradesTwo.csv"))
                                                        trades.Add(trade);
                                                }));

            tasks.Add(Task.Factory.StartNew(() =>
                                                {
                                                    foreach (var trade in new TradeLoader().GetTrades(@"..\..\TradeData\TradesThree.csv"))
                                                        trades.Add(trade);
                                                }));
            Task.WaitAll(tasks.ToArray());

            foreach (var trade in trades)
            {
                trade.PriceChanged += PriceChanged;
                Console.WriteLine(trade.ToString());
            }

            stopwatch.Stop();
            Console.WriteLine("LOAD END: {0} msecs", stopwatch.ElapsedMilliseconds);

            Console.ReadLine();
            PriceTicker.Instance.Dispose();
        }

        private static void PriceChanged(object sender, PriceChangedEventArgs e)
        {
            var trade = sender as Trade;
            if (trade == null)
                throw new ArgumentException("sender is not a Trade object");

            Console.WriteLine(trade.ToString());
        }
    }
}
